package com.example.springboot.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.example.springboot.controller.request.MessagePageRequest;
import com.example.springboot.entity.Message;
import com.example.springboot.mapper.MessageMapper;
import com.example.springboot.service.IMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MessageService implements IMessageService{
    @Autowired
    MessageMapper mapper;

    @Override//获取所有用户信息
    public List<Message> list() {
        return mapper.list();
    }

    @Override//获取分页用户信息,利用插件实现
    public PageInfo<Message> page(MessagePageRequest messagePageRequest){
        PageHelper.startPage(messagePageRequest.getPageNum(), messagePageRequest.getPageSize());
        List<Message> messages = mapper.listByCondition(messagePageRequest);
        return new PageInfo<>(messages);
    }

    @Override//添加新留言
    public void save(Message message) {
        message.setCreatetime(new Date());
        mapper.save(message);
    }

    @Override
    public Message getById(Integer id) {
        return mapper.getById(id);
    }

    @Override
    public void updateById(Message message) {
        mapper.updateById(message);
    }

    @Override
    public void deleteById(Integer id) {
        mapper.deleteById(id);
    }

    @Override
    public void handleReply(Message message) {
        String reply = message.getReply();
        if (reply == null) {
            return;
        }
        Integer id = message.getId();
        Message dbMessage = mapper.getById(id);
        dbMessage.setReply(reply);
        dbMessage.setReplytime(new Date());
        mapper.updateById(dbMessage);
    }


}
